/**
 * API服务层
 * 封装所有接口调用
 * @author min
 */

import request, {
	http
} from './request.js'

// 用户相关API
export const userApi = {
	// 用户登录
	login: (data) => request.post('/api/auth/login', data),
	// 用户登录
	wxlogin: (code) => request.post('/api/auth/wxLogin', code),
	// 用户注册
	register: (data) => request.post('/api/auth/register', data),
	// 修改密码
	changePssword: (data) => request.put('/api/auth/password', data),
	// 绑定手机号
	bindPhone: (code) => request.put('/api/auth/bindPhone', code),
	// 解绑手机号
	unbindPhone: (data) => request.put('/api/auth/unbindPhone', data),
	// 绑定微信openid
	bindWechat: (data) => request.put('/api/auth/bindWechat', data),



	// 获取用户信息
	getUserInfo: () => request.get('/api/user/info'),
	// 更新用户信息
	updateUserInfo: (data) => request.put('/api/user/info', data),
	// 上传头像
	uploadAvatar: (filePath) => request.upload('/api/user/avatar', filePath),

}

// 房子相关API
export const hoiuseApi = {
	// 新增
	publish: (data) => request.post('/api/house/add', data),

	// 删除
	delete: (id) => request.delete(`/api/house/${id}`),

	// 获取所有
	getAll: () => request.get('/api/house/published'),

	// 条件获取所有
	getList: (data) => request.get('/api/house/search', data),

	// 获取个人所有
	getByPublishId: (publisherId) => request.get(`/api/house/publisher/${publisherId}`),

	// 获取附近房源
	getNearbyList: (data) => request.get(`/api/house/nearby`,data),

	// 获取房子详情
	getDetail: (id) => request.get(`/api/house/details/${id}`)
}

// 需求相关API
export const demandApi = {

	// 发布需求
	publish: (data) => request.post('/api/demand/add', data),
	// 删除需求
	delete: (id) => request.delete(`/api/demand/${id}`),
	// 获取个人需求列表
	getByPublishId: (publisherId) => request.get(`/api/house/demand/${publisherId}`),

	// 获取需求列表
	getList: () => request.get('/api/demand/list'),

	getList: (params) => request.get('/api/car/list', {
		data: params
	}),
	// 获取汽车详情
	getDetail: (carId) => request.get(`/api/car/${carId}`),
	// 发布汽车
	publish: (data) => request.post('/api/car/add', data),

	// 更新汽车信息
	update: (carId, data) => request.put(`/api/car/${carId}`, data),

	// 删除汽车
	delete: (carId) => request.delete(`/api/car/${carId}`),

	// 搜索汽车
	search: (params) => request.get('/api/car/search', {
		data: params
	}),
	// 获取我的发布
	getMyPublications: () => request.get('/api/car/my-publications'),

	// 收藏汽车
	favorite: (carId) => request.post(`/api/car/${carId}/favorite`),

	// 取消收藏
	unfavorite: (carId) => request.delete(`/api/car/${carId}/favorite`),

	// 获取收藏列表
	getFavorites: () => request.get('/api/car/favorites')
}

// 认证相关API
export const authApi = {
	// 身份认证
	verifyId: (data) => request.post('/api/auth/id-verify', data),
	// 车主认证
	verifyOwner: (data) => request.post('/api/auth/owner-verify', data),
	// 车商认证
	verifyDealer: (data) => request.post('/api/auth/dealer-verify', data),
	// 获取认证状态
	getVerifyStatus: () => request.get('/api/auth/verify-status')
}

// 反馈相关API
export const feedbackApi = {
	// 提交反馈
	submit: (data) => request.post('/api/feedback/submit', data),

	// 获取反馈列表
	getList: () => request.get('/api/feedback/list'),

	// 删除反馈
	delete: (feedbackId) => request.delete(`/api/feedback/${feedbackId}`)
}

// 通用API
export const commonApi = {
	// 上传文件
	uploadFile: (filePath, config = {}) => request.upload('/api/upload/file', filePath, config),

	// 上传图片
	uploadImage: (filePath) => request.upload('/api/upload/image', filePath),

	// 获取城市列表
	getCities: () => request.get('/api/common/cities'),

	// 获取配置信息
	getConfig: () => request.get('/api/common/config')
}

// 导出所有API
export default {
	user: userApi,
	house: hoiuseApi,
	demand: demandApi,
	auth: authApi,
	feedback: feedbackApi,
	common: commonApi
}

// 使用示例：
/*
import api from '@/utils/api.js'

// 登录
const login = async () => {
  try {
    const result = await api.user.login({
      username: 'test',
      password: '123456'
    })
    console.log('登录成功:', result)
  } catch (error) {
    console.error('登录失败:', error)
  }
}

// 获取汽车列表
const getCarList = async () => {
  try {
    const result = await api.car.getList({
      page: 1,
      size: 10,
      brand: '宝马',
      price: '10-20'
    })
    console.log('汽车列表:', result)
  } catch (error) {
    console.error('获取失败:', error)
  }
}

// 上传图片
const uploadImage = async (filePath) => {
  try {
    const result = await api.common.uploadImage(filePath)
    console.log('上传成功:', result)
  } catch (error) {
    console.error('上传失败:', error)
  }
}
*/